//全局变量，保存学号或教师号
var user = '';
var flag = $.session.get("shenfen");

if(flag == "user") {
	user = $.session.get("sno");
} else if(flag == "teacher") {
	user = $.session.get("tno");
}

$(function() {

	var urlPath = getSourceURL() + user + ".jpg";

	isRunUrl(urlPath).then(function(data) {
		$('#img_src_4').attr("src", urlPath);
		console.clear();
	}, function(data) {
		console.clear();
		if(flag == "user") {
			$('#img_src_4').attr("src", "./img/student.jpg");
		} else if(flag == "teacher") {
			$('#img_src_4').attr("src", "./img/teacher.jpg");
		}
	})

	$('#img_up').click(function() {
		$('#Modal_').modal({
			keyboard: true,
			show: true
		})
	})

	$('#ok_password').click(function() {
		var old_password = $('#old_password').val();
		var new_password = $('#new_password').val();
		var new_password2 = $('#new_password2').val();
		if(new_password != new_password2) {
			alert('两次密码不一致');
			return;
		} else {
			if($('#new_password').val().length < 6 || $('#new_password2').val().length < 6 || $('#old_password').val().length < 6) {
				alert('密码不能小于6位');
				return;
			} else { //请求后台
				$.ajax({
					xhrFields: {
						withCredentials: true
					},
					url: getFullURL("update-password"), //请求的url地址
					dataType: "json", //返回格式为json
					async: true, //请求是否异步，默认为异步，这也是ajax重要特性
					data: {
						password: old_password,
						newPassword: new_password2
					}, //参数值
					type: "post", //请求方式
					success: function(data) {
						if(data.status == 0) {
							alert("修改成功");

							$.ajax({
								xhrFields: {
									withCredentials: true
								},
								type: "GET",
								url: getFullURL("logout"),
								data: {},
								dataType: "json",
								success: function(data) {
									if(data.status == 0) {
										$.session.remove("shenfen");
										$.session.remove("sno");
										$.session.remove("tno");
										window.location.href = "login.html";
									}
								},
								error: function(err) {
									alert("请求异常，请重新登录！", function() {
										$(location).attr('href', './login.html');
									})
								}
							})
						} else {
							alert("修改失败，旧密码可能是错误的")
						}
					},
					error: function(err) {
						alert("请求异常，请重新登录！", function() {
							$(location).attr('href', './login.html');
						})
					}
				})
			}
		}
	})

	$('#new_password2').blur(function() {
		$(this).css('border-color', '')
		if($(this).val() == '') {
			$(this).css('border-color', 'red')
			$(this).attr('placeholder', '未填写密码')
		} else if($(this).val().length < 6) {
			$(this).css('border-color', 'red')
			alert("密码不能小于6位");
		}
	})

	$('#new_password').blur(function() {
		$(this).css('border-color', '')
		if($(this).val() == '') {
			$(this).css('border-color', 'red')
			$(this).attr('placeholder', '未填写密码')
		} else if($(this).val().length < 6) {
			$(this).css('border-color', 'red')
			alert("密码不能小于6位");
		}
	})

	$('#old_password').blur(function() {
		$(this).css('border-color', '')
		if($(this).val() == '') {
			$(this).css('border-color', 'red')
			$(this).attr('placeholder', '未填写密码')
		} else if($(this).val().length < 6) {
			$(this).css('border-color', 'red')
			alert("密码不能小于6位");
		}
	})
})

function student_update() {
	var myajax = $.ajax({
		xhrFields: {
			withCredentials: true
		},
		url: getFullURL("userInfo"), //请求的url地址
		dataType: "json", //返回格式为json
		async: true, //请求是否异步，默认为异步，这也是ajax重要特性
		data: {
			sno: user
		}, //参数值
		type: "post", //请求方式
		beforeSend: function(data) {
			//请求前的处理
		},
		success: function(data) {
			$('.student-right').html(`
			  <p>姓&emsp;名：&emsp;<span><input type="text" id="name" value="` + data.user.name + `" disabled></span><span class="index" index='student'><a>个人资料></a></span></p>
		      <p class="pad col-lg-6 col-sm-12">学&emsp;校：&emsp;<span><input type="text" value="` + data.user.school.sname + `" disabled></span></p>
		      <p>专业方向：<span><input type="text" id="profession" value="` + data.user.profession + `"></span></p>
		      <p class="pad col-lg-6 col-sm-12">学&emsp;号：&emsp;<span><input class="inp" type="text" value="` + data.user.sno + `" disabled></span></p>
		      <p class="pad col-lg-6 col-sm-12">宿&emsp;舍：&emsp;<span><input type="text" id="dorm" value="` + nullStr(data.user.dorm) + `"></span></p>
		      <p class="pad col-lg-6 col-sm-12">性&emsp;别：&emsp;<span><input type="radio" name="sex" id="nan" value="男" class='sex'>男&emsp;<input type="radio" name="sex" id="nv" value="女" class='sex'>女</span></p>
		      <p class="pad col-lg-6 col-sm-12">民&emsp;族：&emsp;<span><input type="text" id="nation" value="` + nullStr(data.user.nation) + `"></span></p>
		      <p class="pad col-lg-6 col-sm-12">籍&emsp;贯：&emsp;<span><input type="text" id="place" value="` + nullStr(data.user.place) + `"></span></p>
		      <p class="pad col-lg-6 col-sm-12">生&emsp;日：&emsp;<span><input type="text" id="born" placeholder="请选择日期"></span></p>
		      <p class="pad col-lg-6 col-sm-12">年&emsp;龄：&emsp;<span><input type="text" id="age" value="` + nullStr(data.user.age) + `"></span></p>
		      <p class="pad col-lg-6 col-sm-12">身&emsp;高：&emsp;<span><input type="text" id="height" value="` + nullStr(data.user.height) + `"></span></p>
		      <p class="pad col-lg-12 col-sm-12">个人评价：<span><textarea id="evaluate">` + nullStr(data.user.evaluate) + `</textarea></span></p>
		      <p class="pad col-lg-12 col-sm-12">技能描述：<span><textarea id="skill">` + nullStr(data.user.skill) + `</textarea></span></p>
		      <p class="pad col-lg-12 col-sm-12">求职意向：<span><textarea id="intention">` + nullStr(data.user.intention) + `</textarea></span></p>
		      <p class="pad col-lg-6 col-sm-12"><span >电&emsp;话：&emsp;</span><input type="text" id="phone" value="` + nullStr(data.user.phone) + `"></p>
		      <p class="pad col-lg-6 col-sm-12">微&emsp;信：&emsp;<span><input type="text" id="wechat" value="` + nullStr(data.user.wechat) + `"></span></p>
		      <p class="pad col-lg-6 col-sm-12"><span >邮&emsp;箱：&emsp;</span><input type="text" id="email" value="` + nullStr(data.user.email) + `"></p>
		      <p class="pad col-lg-6 col-sm-12">企&emsp;鹅：&emsp;<span><input type="text" id="qq" value="` + nullStr(data.user.qq) + `"></span></p>
		      <p id="btn_gro"><button id='btn_password' class="btn btn-primary">修改密码</button><button id='btnn' class="btn btn-primary">确认修改</button><p>
		    `);

			if(data.user.sex == "男") {
				$('#nan').attr("checked", "checked");
			} else if(data.user.sex == "女") {
				$('#nv').attr("checked", "checked");
			} else {
				$('#nan').attr("checked", "checked");
			}

			var born = data.user.born;
			born = new Date(born).format("yyyy-MM-dd");

			laydate.render({
				elem: '#born',
				value: born
			});

			$('.index').click(function() {
				$('.center').html('');
				$('.nav li').removeClass('active')
				$('.center').load('./' + $(this).attr('index') + '.html');
			})
		},
		error: function(err) {
			alert("请求异常，请重新登录！", function() {
				$(location).attr('href', './login.html');
			})
		}
	});

	$.when(myajax).done(function() {
		$('#btn_password').click(function() {
			$('#bm_password').modal({
				keyboard: true,
				show: true
			})
		})

		$('#btnn').click(function() {
			$.ajax({
				xhrFields: {
					withCredentials: true
				},
				url: getFullURL("update-user"), //请求的url地址
				dataType: "json", //返回格式为json
				async: true, //请求是否异步，默认为异步，这也是ajax重要特性
				data: {
					name: $('#name').val(),
					sex: $("input[type='radio']:checked").val(),
					profession: $('#profession').val(),
					sno: user,
					sid: "xiangsihu",
					nation: $('#nation').val(),
					born: $('#born').val(),
					age: $('#age').val(),
					height: $('#height').val(),
					dorm: $('#dorm').val(),
					place: $('#place').val(),
					evaluate: $('#evaluate').val(),
					skill: $('#skill').val(),
					intention: $('#intention').val(),
					phone: $('#phone').val(),
					wechat: $('#wechat').val(),
					email: $('#email').val(),
					qq: $('#qq').val()
				}, //参数值
				type: "post",
				success: function(data) {
					alert('修改成功')
					//请求成功时处理
				},
				error: function(err) {
					alert("请求异常，请重新登录！", function() {
						$(location).attr('href', './login.html');
					})
				}
			})
		})
	});
}

function teacher_update() {
	var myajax = $.ajax({
		xhrFields: {
			withCredentials: true
		},
		url: getFullURL("teacher-info"), //请求的url地址
		dataType: "json", //返回格式为json
		async: true, //请求是否异步，默认为异步，这也是ajax重要特性
		data: {
			sno: user
		}, //参数值
		type: "post",
		success: function(data) {
			var zige = '';
			if(data.teacher.type == 1) {
				zige = "毕设老师";
			} else {
				zige = "普通老师";
			}

			$('.student-right').html(`
				<p class="pad col-lg-12 col-sm-12">账&emsp;&emsp;号：&emsp;<span><input class="inp" type="text" value="` + data.teacher.tno + `" disabled></span><span class='index' index='student'><a>查看资料></a></span></p>
				<p class="pad col-lg-12 col-sm-12">姓&emsp;&emsp;名：&emsp;<span><input type="text" id="name" value="` + data.teacher.name + `"></span></p>
				<p class="pad col-lg-12 col-sm-12">资&emsp;&emsp;格：&emsp;<span><input type="text" id="zige" value="` + zige + `" disabled></span></p>
		      	<p class="pad col-lg-12 col-sm-12" style="padding-bottom: 10px;margin-bottom: 0;border-bottom: 0;">研究方向：&emsp;<span><input type="text" id="profession" value="` + nullStr(data.teacher.profession) + `"></span></p>
		     	<p class="pad col-lg-12 col-sm-12" style="padding-bottom: 10px;margin-bottom: 0;border-bottom: 0;"><span>电&emsp;&emsp;话：&emsp;</span><input type="text" id="phone" value="` + nullStr(data.teacher.phone) + `"></p>
		      	<p class="pad col-lg-12 col-sm-12"><span>邮&emsp;&emsp;箱：&emsp;</span><input type="text" id="email" value="` + nullStr(data.teacher.email) + `"></p>
		      	<p style="float:left;margin-top: 20px;margin-bottom: 20px;"><button id='btn_password' class="btn btn-primary">修改密码</button><button id='btnn' class="btn btn-primary">确认修改</button></p>
		      `)

			$('.index').click(function() {
				$('.center').html('');
				$('.nav li').removeClass('active')
				$('.center').load('./' + $(this).attr('index') + '.html');
			})
		},
		error: function(err) {
			alert("请求异常，请重新登录！", function() {
				$(location).attr('href', './login.html');
			})
		}
	});

	$.when(myajax).done(function() {

		$('#btn_password').click(function() {
			$('#bm_password').modal({
				keyboard: true,
				show: true
			})
		})

		$('#btnn').click(function() {
			$.ajax({
				xhrFields: {
					withCredentials: true
				},
				url: getFullURL("update-teacher"), //请求的url地址
				dataType: "json", //返回格式为json
				async: true, //请求是否异步，默认为异步，这也是ajax重要特性
				data: {
					name: $('#name').val(),
					profession: $('#profession').val(),
					phone: $('#phone').val(),
					email: $('#email').val()
				}, //参数值
				type: "post",
				success: function(data) {
					alert('修改成功')
					//请求成功时处理
				},
				error: function(err) {
					alert("请求异常，请重新登录！", function() {
						$(location).attr('href', './login.html');
					})
				}
			})
		})
	});
}

if(flag == "user") {
	student_update();
} else if(flag == "teacher") {
	teacher_update();
	$('#img_up').css("display", "none");
}

$("#file_up").click(function() {
	if($('#fileName').val() == null || $('#fileName').val() == "") {
		alert("还未选择头像！");
		return;
	}

	var formData = new FormData($("#form_")[0]);

	$.ajax({
		xhrFields: {
			withCredentials: true
		},
		type: "POST",
		url: getFullURL('user/up_img'),
		data: formData,
		dataType: 'json',
		cache: false,
		processData: false, // 不加会报错
		contentType: false, // 不加会报错
		success: function(data) {
			if(data.status == 0) {
				alert("头像保存成功！", function() {
					$.session.set("home_path", 'Modifyinformation');
					window.location.href = "./home.html";
					//					$('.center').html('');
					//					$('.center').load('./Modifyinformation.html');
				})
			}
		},
		error: function(err) {
			alert("请求异常，请重新登录！", function() {
				$(location).attr('href', './login.html');
			})
		}
	})
});